package com.cy.dao.impl;

import com.cy.dao.AccountDao;
import com.cy.entity.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;

/**
 * @Author : 残影
 * @Date: 2020-09-14 09:46
 */
@Service("accountDao")
public class AccountDaoImpl implements AccountDao {
    @Autowired
    private QueryRunner runner;

    @Override
    public void save(Account account) throws SQLException {
        runner.update("insert into account(name,money)values(?,?)", account.getName(), account.getMoney());
    }

    @Override
    public void update(Account account) throws SQLException {
        runner.update("update account set name=?,money=? where id=?", account.getName(), account.getMoney(), account.getId());
    }

    @Override
    public void delete(Integer accountId) throws SQLException {
        runner.update("delete from account where id=?", accountId);
    }

    @Override
    public Account findById(Integer accountId) throws SQLException {
        return runner.query("select * from account where id=?", new
                BeanHandler<Account>(Account.class), accountId);
    }

    @Override
    public List<Account> findAll() throws SQLException {
        return runner.query("select * from account ", new BeanListHandler<Account>(Account.class));
    }

    public void setRunner(QueryRunner runner) {
        this.runner = runner;
    }

    public QueryRunner getRunner() {
        return runner;
    }
}
